package com.jh.wdbc.service;

import com.nhncorp.dbpropmanager.anttask.RemoteDBPropertiesGetter;
import com.nhncorp.dbpropmanager.model.GlobalConfig;
import com.nhncorp.dbpropmanager.model.PropertyModel;

public class DBPMUtil {
	public static PropertyModel getDBProperty(String phase, String dbid) throws Exception {
		GlobalConfig.setServerDomain(getServerUrl(phase));
		String[] dbids = dbid.split("\\.");
		StringBuilder sb = new StringBuilder(dbids[1]);
		for (int i = 2; i < dbids.length; i++) {
			sb.append(".").append(dbids[i]);
		}

		return RemoteDBPropertiesGetter.getDataSourceProperty(dbids[0], sb.toString());
	}

	private static String getServerUrl(String phase) {
		if ("real".equals(phase)) {
			return "dbpropmanager.admin.nhncorp.com";
		} else {
			return phase + "-dbpropmanager.admin.nhncorp.com";
		}
	}

	public static String getDBType(String driver) {
		if (driver.contains("Oracle")) {
			return "Oracle";
		}
		if (driver.contains("mysql")) {
			return "mysql";
		}
		return "others";
	}

	public static String[] split(String dbName) {
		int index = dbName.indexOf(".");
		String[] result = new String[2];
		result[0] = dbName.substring(0, index);
		result[1] = dbName.substring(index + 1, dbName.length());
		return result;
	}

	public static boolean isDBPM(String phase) {
		if ("dev".equals(phase) || "alpha".equals(phase)) {
			return true;
		}
		return false;
	}
}
